package com.example.gpssystem.controller;

import com.example.gpssystem.service.AddressService;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/addresses")
@CrossOrigin(origins = "http://localhost:5173")
public class AddressController {
    @Autowired
    private AddressService addressService;

    @GetMapping("/{userId}")
    public ResponseEntity<List<String>> getAddresses(@PathVariable String userId) {
        return ResponseEntity.ok(addressService.getCommonAddresses(userId));
    }

    @PostMapping("/{userId}")
    public ResponseEntity<Void> addAddress(
            @PathVariable String userId,
            @Valid @RequestBody AddressRequest request) {
        addressService.addCommonAddress(userId, request.getAddress());
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @DeleteMapping("/{userId}")
    public ResponseEntity<Void> deleteAddress(
            @PathVariable String userId,
            @RequestParam String address) {
        addressService.deleteAddress(userId, address);
        return ResponseEntity.noContent().build();
    }

    @Data
    static class AddressRequest {
        @NotBlank
        private String address;
    }
}